const app = getApp();
let ServiceUrl = app.globalData.ServiceUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nurseList: [],
    company_id: '',
    order_id: ''
  },
   //提示封装
   showModal(msg){
    wx.showModal({
      content:msg,
      showCancel: false,
      confirmColor: 'skyblue',
    })
  },
  onSearch(e){
    console.log(e.detail)
    let that = this
    wx.request({
      url: ServiceUrl + 'searchNurseInfo',
      data: {
        user_name: e.detail,
        company_id: that.data.company_id
      },
      method: "get",
      success: function(res){
        console.log(res)
        if(res.data.state == 200){
          that.setData({nurseList: res.data.data})
        }
      },
      fail: function(){
        console.log('发送请求失败')
      }
    })
  },
  onChange(e){
    console.log(e.detail)
    let that = this
    wx.request({
      url: ServiceUrl + 'searchNurseInfo',
      data: {
        user_name: e.detail,
        company_id: that.data.company_id
      },
      method: "get",
      success: function(res){
        console.log(res)
        if(res.data.state == 200){
          that.setData({nurseList: res.data.data})
        }
      },
      fail: function(){
        console.log('发送请求失败')
      }
    })
  },
  send: function(e){
    let that = this
//    console.log(e.currentTarget.id)
    wx.showModal({
      title:"提示",
      content:'是否确认派单？',
      showCancel: false,
      confirmColor: 'skyblue',
      success: function(res){
        if(res.confirm){
          wx.request({
            url: ServiceUrl + 'sendAccompanyMsg',
            data: {
              order_id: that.data.order_id,
              user_id: e.currentTarget.id
            },
            method: "get",
            success: function(res){
              console.log(res)
              if(res.data.state == 200){
                that.showModal(res.data.msg)
                that.getNurseList(that.data.company_id)
              }else{
                that.showModal(res.data.msg)
              }
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let that = this
    that.setData({
      company_id: options.company_id,
      order_id: options.order_id
    })
    that.getNurseList(options.company_id);
  },
  /**
   * 获取本公司可接单的陪诊人员列表
   * @param {*} companyId 
   */
  getNurseList:function(companyId){
    let that = this
    wx.request({
      url: ServiceUrl + 'getNurseList',
      data: {company_id: companyId},
      success: function(res){
        console.log(res.data.data)
        if(res.data.state == 200){
          that.setData({nurseList: res.data.data})
        }
      },
      fail:function(){
        console.log('发送请求失败')
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this
    wx.stopPullDownRefresh({
      success: (res) => {
        that.getNurseList(that.data.company_id);
      },
    },3000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})